/**
 * 
 */
package com.gson.util;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

import org.apache.commons.lang3.StringUtils;

/**
 * @作者 Stone
 * @创建日期 2014-6-20 上午11:24:48
 * @版本 V1.0
 * @类说明
 */
public class FileUtil {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		String picUrl = "http://api.map.baidu.com/staticimage?center=121.387072,31.168272&width=640&height=320&zoom=17&markers=121.387072,31.168272&markerStyles=l,A";
		// String picUrl =
		// "http://114chahao.duapp.com/static/images/staticimage.png";
		// 目标图片路径
		String tarUrl = "D:/p2.jpg";
		//ioImage(picUrl, tarUrl);
		//savePosImage(picUrl);
		
		//String path = "/tmp/jetty-0.0.0.0-18080-root.war-_-any-/webapp/WEB-INF/classes/";
		//path = path.substring(0, path.indexOf("/WEB-INF"));
		
		delFolder("c:/bb");
	    System.out.println("deleted");
		
	}
	
	/**
	 * 保存位置图片至图片目录
	 * @param srcUrl
	 */
	public static void savePosImage(String srcUrl, String tarUrl) {
		ioImage(srcUrl, tarUrl);
	}

	/**
	 * 
	 * @param srcUrl 源图片路径
	 * @param tarUrl 源图片路径
	 */
	private static void ioImage(String srcUrl, String tarUrl) {
		
		String serverPath = getImagePath();
		
		String prePath = serverPath + JodaTime.getPreDay();
		String path = serverPath + JodaTime.getToday();

		try {
			// 目标图片目录
			File imageFolder = new File(path);
			
			if(!imageFolder.exists()) {
				//创建今日目标图片目录
				imageFolder.mkdir();
				
				//删除昨日目标图片目录
				delFolder(prePath);
			}

			// 生成读取图片到内存的输入流
			InputStream finput = HttpUtil.httpRequest(srcUrl);
			
			// 目标图片文件
			File imagefile = new File(tarUrl);
			
			// 生成从内存将图片输出到磁盘的输出流
			FileOutputStream foutput = new FileOutputStream(imagefile);
			BufferedOutputStream bos = new BufferedOutputStream(foutput);

			int b;
			while (true) {
				if (finput.available() < 1024) {
					//
					while ((b = finput.read()) != -1) {

						bos.write(b);
					}
					break;
				} else {

					b = finput.read();
					bos.write(b);
				}
			}
			finput.close();
			bos.close();
			foutput.close();

		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}
	
	public static String getImagePath() {
		//String classPath = FileUtil.class.getResource("/").getPath().replaceAll("%20", " ");
		String path = "/tmp/jetty-0.0.0.0-18080-root.war-_-any-/webapp/static/images/";
		return path;
	}
	
	/**
	 * 创建文件
	 * @param file
	 */
	public static File createFile(String filePath) {
		// File fDir = new File(File.separator); //根目录
		// File f = new File(fDir, strFile);
		String[] fileArray = filePath.split("/");
		String filePathTemp = "";
		for (int i = 0; i < fileArray.length - 1; i++) {
			filePathTemp += fileArray[i];
			filePathTemp += File.separator;
		}
		filePathTemp += fileArray[fileArray.length - 1];
		if (filePathTemp.equals("")) {
			filePathTemp = filePath;
		}
		File f = new File(filePathTemp);
		File pf = f.getParentFile();
		if (null != pf && !pf.exists()) {
			pf.mkdirs();
		}
		try {
			f.createNewFile();
		} catch (IOException e) {
			e.printStackTrace();
			return f;
		}
		return f;
	}
	
	/**
	 * 删除文件夹
	 * @param folderPath 文件夹完整绝对路径
	 */
	public static void delFolder(String folderPath) {
		try {
			delAllFile(folderPath); // 删除完里面所有内容
			String filePath = folderPath;
			filePath = filePath.toString();
			java.io.File myFilePath = new java.io.File(filePath);
			myFilePath.delete(); // 删除空文件夹
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 删除指定文件夹下所有文件
	 * @param path	文件夹完整绝对路径
	 * @return
	 */
	public static boolean delAllFile(String path) {
		boolean flag = false;
		File file = new File(path);
		if (!file.exists()) {
			return flag;
		}
		if (!file.isDirectory()) {
			return flag;
		}
		String[] tempList = file.list();
		File temp = null;
		for (int i = 0; i < tempList.length; i++) {
			if (path.endsWith(File.separator)) {
				temp = new File(path + tempList[i]);
			} else {
				temp = new File(path + File.separator + tempList[i]);
			}
			if (temp.isFile()) {
				temp.delete();
			}
			if (temp.isDirectory()) {
				delAllFile(path + "/" + tempList[i]);// 先删除文件夹里面的文件
				delFolder(path + "/" + tempList[i]);// 再删除空文件夹
				flag = true;
			}
		}
		return flag;
	}

}
